package com.ciwong.epaper.modules.epaper.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.ciwongwrite.utils.CWUtils;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.evaluate.bean.DKWorkDays;
import com.ciwong.epaper.modules.evaluate.bean.DkWorkDetail;

import java.util.Calendar;

public class UserDKWorkWeekAdapter extends BaseAdapter {


    private Context mContext;

    private DkWorkDetail dkWorkDetail;

    private RelativeLayout relativeLayout;

    private ImageView imageView;

    private TextView isDKText;

    private TextView weekText;

    private TextView dateText;

    private TextView isStartText;

    public UserDKWorkWeekAdapter(Context mContext, DkWorkDetail dkWorkDetail) {
        this.mContext = mContext;
        this.dkWorkDetail = dkWorkDetail;
    }


    @Override
    public int getCount() {
        return dkWorkDetail.getDkworkdays().size();
    }

    @Override
    public Object getItem(int i) {
        return dkWorkDetail.getDkworkdays().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = View.inflate(mContext, R.layout.activity_user_dk_work_child, null);
        relativeLayout = view.findViewById(R.id.user_dk_child_layout);
        imageView = view.findViewById(R.id.user_dk_isdk_img);
        isDKText = view.findViewById(R.id.user_dk_isdk_text);
        weekText = view.findViewById(R.id.user_dk_child_week_text);
        dateText = view.findViewById(R.id.user_dk_child_date);
        isStartText = view.findViewById(R.id.user_dk_child_isStart_text);

        DKWorkDays dkWorkDays = dkWorkDetail.getDkworkdays().get(i);
        long dkDate = dkWorkDays.getDkDate();
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTimeInMillis(dkDate * 1000);
        cal2.setTimeInMillis(System.currentTimeMillis());
        if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)) {
            dateText.setText("今天");
        } else {
            dateText.setText(CWUtils.formatDate6(dkDate * 1000));
        }
        weekText.setText(CWUtils.formatDateToWeek2(dkDate * 1000));
        if (dkWorkDetail.getIsStart() == 0) {
            relativeLayout.setBackground(mContext.getResources().getDrawable(R.drawable.user_dk_button));
            isDKText.setVisibility(View.GONE);
            weekText.setVisibility(View.GONE);
            imageView.setVisibility(View.GONE);
            isStartText.setVisibility(View.VISIBLE);
        } else if (dkWorkDetail.getIsStart() == 2) {
            if (dkWorkDays.getIsDk() == 1) {
                relativeLayout.setBackground(mContext.getResources().getDrawable(R.drawable.user_dk_button));
                imageView.setVisibility(View.VISIBLE);
                weekText.setVisibility(View.VISIBLE);
                isDKText.setVisibility(View.GONE);
                isStartText.setVisibility(View.GONE);
            } else {
                imageView.setVisibility(View.GONE);
                relativeLayout.setBackground(mContext.getResources().getDrawable(R.drawable.user_dk_button));
                isDKText.setText("未完成");
                weekText.setVisibility(View.VISIBLE);
                isDKText.setTextColor(mContext.getResources().getColor(R.color.viewfinder_laser));
                isDKText.setVisibility(View.VISIBLE);
                isStartText.setVisibility(View.GONE);
                relativeLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    }
                });
            }
        } else {
            if (dkWorkDays.getIsDk() == 1) {
                relativeLayout.setBackground(mContext.getResources().getDrawable(R.drawable.user_dk_button));
                imageView.setVisibility(View.VISIBLE);
                weekText.setVisibility(View.VISIBLE);
                isDKText.setVisibility(View.GONE);
                isStartText.setVisibility(View.GONE);
            } else {
                if (dkWorkDetail.getTodayIsDkDay() == 1 && dkWorkDetail.getTodayDate().getDkDateNum() == dkWorkDays.getDkDateNum()) {
                    imageView.setVisibility(View.GONE);
                    isDKText.setText("待完成");
                    dateText.setText("今天");
                    weekText.setVisibility(View.VISIBLE);
                    relativeLayout.setBackground(mContext.getResources().getDrawable(R.drawable.user_dk_button2));
                    isDKText.setTextColor(mContext.getResources().getColor(R.color.white));
                    weekText.setTextColor(mContext.getResources().getColor(R.color.white));
                    isDKText.setVisibility(View.VISIBLE);
                    isStartText.setVisibility(View.GONE);
                } else {
                    if (dkWorkDetail.getTodayDate().getDkDateNum() >= dkWorkDays.getDkDateNum()) {
                        imageView.setVisibility(View.GONE);
                        relativeLayout.setBackground(mContext.getResources().getDrawable(R.drawable.user_dk_button));
                        if (dkWorkDetail.getSubmitMode() == 0) {
                            isDKText.setText("可补卡");
                            isDKText.setTextColor(mContext.getResources().getColor(R.color.light_orange));
                        } else {
                            isDKText.setText("未完成");
                            isDKText.setTextColor(mContext.getResources().getColor(R.color.viewfinder_laser));
                        }
                        weekText.setVisibility(View.VISIBLE);
                        isDKText.setVisibility(View.VISIBLE);
                        isStartText.setVisibility(View.GONE);
                    } else {
                        relativeLayout.setBackground(mContext.getResources().getDrawable(R.drawable.user_dk_button));
                        isDKText.setText("暂未开始");
                        isDKText.setVisibility(View.GONE);
                        weekText.setVisibility(View.GONE);
                        imageView.setVisibility(View.GONE);
                        isStartText.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
        return view;
    }

}
